/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.hualu.zks.CommonUtils;

import java.math.BigDecimal;

/**
 *
 * @author Administrator
 */
public class CommonUtils {
    
     // 秒数 转成 时：分：秒
    public String secToTime(int time) {
        String timeStr = null;
        int hour = 0;
        int minute = 0;
        int second = 0;
        if (time <= 0)
            return "00:00";
        else {
            minute = time / 60;
            if (minute < 60) {
                second = time % 60;
                timeStr = unitFormat(minute) + ":" + unitFormat(second);
            } else {
                hour = minute / 60;
                if (hour > 99)
                    return "99:59:59";
                minute = minute % 60;
                second = time - hour * 3600 - minute * 60;
                timeStr = unitFormat(hour) + ":" + unitFormat(minute) + ":" + unitFormat(second);
            }
        }
        return timeStr;
    }
     public static String unitFormat(int i) {
        String retStr = null;
        if (i >= 0 && i < 10)
            retStr = "0" + Integer.toString(i);
        else
            retStr = "" + i;
        return retStr;
    }
    
    //字节转kb，mb
    public static String bytes2kb(long bytes) {  
        float returnValue = 0;
        BigDecimal filesize = new BigDecimal(bytes);  
        BigDecimal kilobyte = new BigDecimal(1024);
        BigDecimal megabyte = new BigDecimal(1024 * 1024);  
        BigDecimal gigabyte = new BigDecimal(1024 * 1024 * 1024);
        
        returnValue = filesize.divide(gigabyte, 2, BigDecimal.ROUND_UP)  
                .floatValue();  
        if (returnValue > 1)
            return (returnValue + "GB");
        
        returnValue = filesize.divide(megabyte, 2, BigDecimal.ROUND_UP)  
                .floatValue();  
        if (returnValue > 1)
            return (returnValue + "MB");
        
        returnValue = filesize.divide(kilobyte, 2, BigDecimal.ROUND_UP)  
                .floatValue();  
        return (returnValue + "KB");  
    }
    
    //判断磁盘空间大小,单位B
    public long Freespace(String location) {
        //java.io.File file = new java.io.File("c:");
        java.io.File file = new java.io.File(location);
        long freeSpace = file.getFreeSpace();
        //System.out.println("剩余空间大小 : " + freeSpace);
        return freeSpace;
    }
}
